import json
import urllib.parse
import urllib.request


class VoiceApi:
    ACCOUNT = 'VM04789982'
    PASSWORD = 'abd4f332c0057424e20a5e12311986f9'
    FORMAT = 'json'

    @staticmethod
    def call_user(mobile, content_order, contents):
        contents_dict = {
            1: "你好{}，领星系统和爱创系统中已将用户{}删除，用户ID为{}，请查看有无异常。".format(contents[0],
                                                                                             contents[1],
                                                                                             contents[2]),
            2: "你好{}，领星系统中新增加用户{}，用户ID为{}，请在爱创系统中为其分配角色。".format(contents[0],
                                                                                             contents[1],
                                                                                             contents[2]),
            3: "你好{}，领星系统{}的{}由{}变为{}，请查看有无异常。".format(contents[0],
                                                                        contents[1],
                                                                        contents[2],
                                                                        contents[3],
                                                                        contents[4]),
            4: "你好{}，爱创系统已将{}删除，请查看有无异常。".format(contents[0],
                                                                  contents[1]),
            5: "你好{}，领星系统中新添加店铺，店铺名称为{}，请在爱创系统中为其分配店铺负责人。".format(contents[0],
                                                                                                   contents[1])
        }

        url = 'http://api.vm.ihuyi.com/webservice/voice.php?method=Submit'
        values = {
            'account': VoiceApi.ACCOUNT,
            'password': VoiceApi.PASSWORD,
            'format': VoiceApi.FORMAT,
            'mobile': mobile,
            'content': contents_dict[content_order],
        }
        data = urllib.parse.urlencode(values).encode(encoding='UTF8')
        req = urllib.request.Request(url, data)
        response = urllib.request.urlopen(req)
        res = response.read()
        decoded_data = res.decode('utf-8')
        result = json.loads(decoded_data)
        print(result)
        print("----------------------------------")
        return result


if __name__ == '__main__':
    mobile = "15191009287"
    content_order = 1
    contents = ("何总", "陈荣博", "123", "", "")
    VoiceApi.call_user(mobile, content_order, contents)
